package com.zhuoyue.biz.core.command.cmd.common.jointcontrol;


import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.command.constant.LianKongBangDingPeiZhiControlCmdDeal;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @author Administrator
 */
public class Jointcontrol {
    private static final String OD_0FAA = "0FAA";
    private static final String INDEX_ID = "indexId";
    private static final String INDEX = "index";

    public static String control(DeviceAndGatewayDO sourceDevice, DeviceAndGatewayDO goalDevice, ControlReq controlReq) {

        String cmd = "";
        switch (controlReq.getControlType()) {
            //添加联控信息
            case LianKongBangDingPeiZhiControlCmdDeal.ControlType.ADD:
                cmd = getAddCmd(sourceDevice, goalDevice, controlReq);
                break;
            //删除联控信息
            case LianKongBangDingPeiZhiControlCmdDeal.ControlType.DELETE:
                cmd = getDeleteCmd(sourceDevice, goalDevice, controlReq);
                break;
            //清空联控信息
            case LianKongBangDingPeiZhiControlCmdDeal.ControlType.CLEAR:
                cmd = getClearCmd(sourceDevice, controlReq);
                break;

            default:
                return null;

        }


        return cmd;
    }


    /**
     * 获取联控绑定新增命令
     *
     * @param
     * @return
     */
    private static String getAddCmd(DeviceAndGatewayDO sourceDevice, DeviceAndGatewayDO goalDevice, ControlReq controlReq) {


        if (OD_0FAA.equals(sourceDevice.getOdIndex())) {
            Map<String, Object> actions = controlReq.getActions();

            String index = "";
            if (controlReq.getActions().get(INDEX_ID) != null) {
                index = controlReq.getActions().get(INDEX_ID).toString();
            }
            String[] s = StringUtils.split(sourceDevice.getProductType(), "_");
            String sourceWayNumber = s[s.length - 1];
            String[] s1 = StringUtils.split(goalDevice.getProductType(), "_");
            String goalWayNumber = s1[s1.length - 1];
            String cmd8 = "0" + index;

            String cmd10 = "0" + sourceWayNumber;

            String cmd11 = "0" + goalWayNumber;


            String cmd3To14 = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_3 +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_4 +
                    sourceDevice.getGatewayMac() +
                    sourceDevice.getZigbeeMac() +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_7 +
                    cmd8 +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_9 +
                    cmd10 +
                    cmd11 +
                    goalDevice.getZigbeeMac() +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_14;

            String cmd2 = CmdUtils.getDateLength(cmd3To14);

            String cmd15 = CmdUtils.getCmdBodySum(cmd3To14);

            String fullCmd = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_1 +
                    cmd2 +
                    cmd3To14 +
                    cmd15 +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_16;

            return fullCmd;
        } else {
            return getNewAddCmd(sourceDevice, goalDevice, controlReq);
        }

    }


    /**
     * 获取联控绑定删除命令
     *
     * @param
     * @return
     */
    private static String getDeleteCmd(DeviceAndGatewayDO sourceDevice, DeviceAndGatewayDO goalDevice, ControlReq controlReq) {

        if (OD_0FAA.equals(sourceDevice.getOdIndex())) {

//
//            String cmd_11 = dealGoalWayNumber(actionsMap.get("goalWayNumber")) + "00";
//
//            String cmd_10 = CmdUtils.intToHex_1(Integer.parseInt(actionsMap.get("sourceWayNumber"))) + "00";
            String indexId = "";
            if (controlReq.getActions().get(INDEX_ID) != null) {
                indexId = controlReq.getActions().get("indexId").toString();
            }

            String cmd3To14 = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_3 +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_4 +
                    sourceDevice.getGatewayMac() +
                    sourceDevice.getZigbeeMac() +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_7 +
                    "0" + indexId +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_9 +
                    LianKongBangDingPeiZhiControlCmdDeal.Clear.CMD_10 +
                    LianKongBangDingPeiZhiControlCmdDeal.Clear.CMD_17 +
                    LianKongBangDingPeiZhiControlCmdDeal.Clear.CMD_10;

            String cmd2 = CmdUtils.getDateLength(cmd3To14);

            String cmd15 = CmdUtils.getCmdBodySum(cmd3To14);

            String fullCmd = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_1 +
                    cmd2 +
                    cmd3To14 +
                    cmd15 +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_16;
            return fullCmd;
        } else {
            return getNewDelateCmd(sourceDevice, goalDevice);
        }
    }


    private static String getClearCmd(DeviceAndGatewayDO sourceDevice, ControlReq controlReq) {

        if (OD_0FAA.equals(sourceDevice.getOdIndex())) {

            String indexId = "";
            if (controlReq.getActions().get(INDEX_ID) != null) {
                indexId = controlReq.getActions().get(INDEX_ID).toString();
            }

            String cmd10 = "FFFF";

            String cmd11 = "0000";

            String cmd12 = "00000000";

            String cmd13 = "0000";

            String cmd14 = "FFFF";


            String cmd3To14 = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_3 +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_4 +
                    sourceDevice.getGatewayMac() +
                    sourceDevice.getZigbeeMac() +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_7 +
                    indexId +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_9 +
                    cmd10 +
                    cmd11 +
                    cmd12 +
                    cmd13 +
                    cmd14;

            String cmd2 = CmdUtils.getDateLength(cmd3To14);

            String cmd15 = CmdUtils.getCmdBodySum(cmd3To14);

            String fullCmd = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_1 +
                    cmd2 +
                    cmd3To14 +
                    cmd15 +
                    LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_16;

            return fullCmd;
        } else {
            return getNewClearCmd(sourceDevice);
        }
    }

    private String dealGoalWayNumber(String goalWayNumber) {

        int way1 = 0;
        int way2 = 0;
        int way3 = 0;
        int way4 = 0;
        int way5 = 0;
        int way6 = 0;
        int way7 = 0;
        int way8 = 0;

        String[] goalWayNumberArr = goalWayNumber.split(",");

        for (int i = 0; i < goalWayNumberArr.length; i++) {
            int way = Integer.parseInt(goalWayNumberArr[i]);
            if (way == 1) {
                way1 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            } else if (way == 2) {
                way2 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            } else if (way == 3) {
                way3 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            } else if (way == 4) {
                way4 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            } else if (way == 5) {
                way5 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            } else if (way == 6) {
                way6 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            } else if (way == 7) {
                way7 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            } else if (way == 8) {
                way8 = LianKongBangDingPeiZhiControlCmdDeal.GoalNodeWayNumber.WAY_ARR[way - 1];
            }
        }

        int goalWayNumberHex = way1 | way2 | way3 | way4 | way5 | way6 | way7 | way8;

        return CmdUtils.intToHexTwo(goalWayNumberHex);

    }

    /**
     * 八路联控绑定新增新增接口
     *
     * @param
     * @return
     */
    private static String getNewAddCmd(DeviceAndGatewayDO sourceDevice, DeviceAndGatewayDO goalDevice, ControlReq controlReq) {

        Map<String, Object> actions = controlReq.getActions();
        String index = "";
        if (controlReq.getActions().get(INDEX) != null) {
            index = controlReq.getActions().get(INDEX).toString();
        }
        index = actions.get(INDEX).toString();
        String[] s = StringUtils.split(sourceDevice.getProductType(), "_");
        String sourceWayNumber = s[s.length - 1];
        String[] s1 = StringUtils.split(goalDevice.getProductType(), "_");
        String goalWayNumber = s1[s1.length - 1];
        String cmd8 = "0" + index;

        String cmd10 = "0" + sourceWayNumber;

        String cmd11 = "0" + goalWayNumber;


        String cmd3To14 = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_3 +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_4 +
                sourceDevice.getGatewayMac() +
                sourceDevice.getZigbeeMac() +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_7 +
                LianKongBangDingPeiZhiControlCmdDeal.Delete.CMD_8 +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_4 +
                //"0100"+"0400"+
                //sourceWayNumber源节点第X路
                transition(cmd10) +
                //goalWayNumber目标节点第X路
                transition(cmd11) +
                //目标节点MAC
                goalDevice.getZigbeeMac() +
                //设备功能ID号
                sourceDevice.getOdIndex() +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_14;

        String cmd2 = CmdUtils.getDateLength(cmd3To14);

        String cmd15 = CmdUtils.getCmdBodySum(cmd3To14);

        String fullCmd = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_1 +
                cmd2 +
                cmd3To14 +
                cmd15 +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_16;

        return fullCmd;
    }

    /**
     * 八路联控绑定删除新增接口
     *
     * @param
     * @return
     */
    private static String getNewDelateCmd(DeviceAndGatewayDO sourceDevice, DeviceAndGatewayDO goalDevice) {


        String[] s = StringUtils.split(sourceDevice.getProductType(), "_");
        String sourceWayNumber = s[s.length - 1];
        String[] s1 = StringUtils.split(goalDevice.getProductType(), "_");
        String goalWayNumber = s1[s1.length - 1];

        String cmd11 = goalWayNumber;

        String cmd10 = sourceWayNumber;


        String cmd3To14 = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_3 +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_4 +
                sourceDevice.getGatewayMac() +
                sourceDevice.getZigbeeMac() +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_7 +
                //LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_8 +
                LianKongBangDingPeiZhiControlCmdDeal.Delete.CMD_8 +//子索引21
                LianKongBangDingPeiZhiControlCmdDeal.Delete.CMD_9 +//绑定子命令ID，删除
                transition(cmd10) +
                transition(cmd11) +
                goalDevice.getZigbeeMac() +
                sourceDevice.getOdIndex() +
                LianKongBangDingPeiZhiControlCmdDeal.Delete.CMD_14;
        //校验和
        String cmd2 = CmdUtils.getDateLength(cmd3To14);
        //帧尾

        String cmd15 = CmdUtils.getCmdBodySum(cmd3To14);

        String fullCmd = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_1 +
                cmd2 +
                cmd3To14 +
                cmd15 +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_16;

        return fullCmd;
    }

    /**
     * 八路清空新增接口
     *
     * @param
     * @return
     */
    private static String getNewClearCmd(DeviceAndGatewayDO sourceDevice) {

        String cmd10 = "FFFF";

        String cmd11 = "FFFF";

        String cmd12 = "FFFFFFFFFFFFFFFF";

        String cmd13 = "FFFF";

        String cmd14 = "FFFF";


        String cmd3To14 = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_3 +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_4 +
                sourceDevice.getGatewayMac() +
                sourceDevice.getZigbeeMac() +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_7 +
                LianKongBangDingPeiZhiControlCmdDeal.Delete.CMD_8 +
                LianKongBangDingPeiZhiControlCmdDeal.Clear.CMD_9 +

                cmd10 +
                cmd11 +
                cmd12 +
                cmd13 +
                cmd14;

        String cmd2 = CmdUtils.getDateLength(cmd3To14);

        String cmd15 = CmdUtils.getCmdBodySum(cmd3To14);

        String fullCmd = LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_1 +
                cmd2 +
                cmd3To14 +
                cmd15 +
                LianKongBangDingPeiZhiControlCmdDeal.Add.CMD_16;

        return fullCmd;
    }

    /**
     * 数据帧转化
     *
     * @param cmdN
     * @return
     */
    private static String transition(String cmdN) {
        int ten = 10;
        int n = Integer.parseInt(cmdN);
        //10进制转化为16进制
        String a = null;
        int cmd16Jz = (int) Math.pow(2, n - 1);
        //把int转为String
        String hex = Integer.toHexString(cmd16Jz);
        int var = Integer.parseInt(hex);
        System.out.println(var);

        if (var > 0 && var < ten) {
            a = "0" + var + "00";
        }
        if (var >= ten) {
            a = var + "00";
        }
        String a16Jz = a.toString();
        return a16Jz;
    }


}
